(function() {with(HappyFarm.util) { with(HappyFarm.service) {

const SCM = ServiceControlManager.getInstance();

function startLogin() {
    HappyFarm.ui.info('[登录中...]');
    request({url: getPref('homepage_url', ''), complete: function(status, xhr) {
        var response = xhr.responseText;
        var m = /http:\/\/xn\.hf\.fminutes\.com[^"]*/.exec(response);

        request({url: m[0], complete: startCrop});
    }});
}

function startCrop() {
    HappyFarm.ui.info('[初始化-1...]');
    HappyFarm.api.crops(startRun);
}

function startRun(){
    HappyFarm.ui.info('[初始化-2...]');
    HappyFarm.api.run(undefined, undefined, startSeed);
}

function startSeed(){
    HappyFarm.ui.info('[初始化-3...]');
    HappyFarm.api.seeds(startFriend);
}

function startFriend(){
    HappyFarm.ui.info('[初始化-4...]');
    HappyFarm.api.friend(function(){
        HappyFarm.ui.info('[启动服务...]');
        SCM.startAll();
    });
}

window.addEventListener('load', function(){

for(var srvName in impl) {
    SCM.registerService(srvName, impl[srvName]);
}

startLogin();
}, false);

}}})();
